package tech.spiro.addrparser.tool;

import com.mysql.cj.jdbc.MysqlDataSource;
import org.apache.commons.cli.HelpFormatter;
import org.apache.commons.cli.Options;
import tech.spiro.addrparser.common.RegionInfo;
import tech.spiro.addrparser.crawler.GetRegionException;
import tech.spiro.addrparser.io.RegionDataInput;
import tech.spiro.addrparser.io.rdbms.RdbmsRegionDataInput;
import tech.spiro.addrparser.io.rdbms.RdbmsSQL;
import tech.spiro.addrparser.parser.Location;
import tech.spiro.addrparser.parser.LocationParserEngine;
import tech.spiro.addrparser.parser.ParserEngineException;

import javax.sql.DataSource;
import java.io.IOException;
import java.sql.Connection;
import java.sql.SQLException;
import java.util.Date;

/**
 * A command-line tool to stream region data from json file to mysql.
 *
 * @author Spiro Huang
 * @since 1.0
 */
public class InitSql {
    private static Options options = new Options();

    static {
        options.addOption("f", "C:\\Users\\admin\\Desktop\\china-region.json", true, "Json region data file path");

        options.addRequiredOption("h", "192.168.1.207", true, "MySQL host");
        options.addOption("p", "3306", true, "MySQL port, default 3306");
        options.addRequiredOption("d", "zacf", true, "MySQL database");
        options.addRequiredOption("u", "root", true, "MySQL user");
        options.addRequiredOption("a", "gawd110", true, "MySQL password");

        options.addOption("t", "region_data", true, "Default: '" + RdbmsSQL.DEFAULT_TABLE_NAME + "', Region data table name");
        options.addOption("i", "init", false, "Init table schema");
    }

    private static void printHelp() {
        HelpFormatter formatter = new HelpFormatter();
        formatter.printHelp("JSONFile2MySQL", options);
    }

    public static void main(String[] args) throws IOException, GetRegionException, ParserEngineException {

//        DataSource dataSource = buildDataSource();
//
//        String tableName = "region_data";
//        try {
//            MySQLTableInit.init(dataSource, tableName);
//        } catch (SQLException e) {
//            e.printStackTrace();
//        }
//        RegionDataInput dataInput = new JSONFileRegionDataInput("C:\\Users\\admin\\Desktop\\china-region.json");
//        RegionDataOutput dataOutput = null;
//        if (tableName == null) {
//            dataOutput = new RdbmsRegionDataOutput(dataSource);
//        } else {
//            dataOutput = new RdbmsRegionDataOutput(dataSource, tableName);
//        }
//
//        IOPipeline pipeline = new IOPipeline(dataInput, dataOutput);
//        pipeline.start();
        // javax.sql.DataSource 数据源
        DataSource ds = buildDataSource();
        // 关系型数据库区域数据输入
        RegionDataInput regionDataInput = new RdbmsRegionDataInput(ds);
        // 如果上述命令如果指定了定制的表名，则该处也要指定表名。
//         RegionDataInput regionDataInput = new RdbmsRegionDataInput(ds, "region_data");
        // 创建并初始化位置解析引擎，一般配置为全局单例
        LocationParserEngine engine = new LocationParserEngine(regionDataInput);
        // 初始化，加载数据，比较耗时
        Date date = new Date();
        engine.init();
        System.out.println("-----》初始化耗时：" + calLastedTime(date, new Date()) + "毫秒");
        Date date1 = new Date();
        // 执行解析操作
        Location location = engine.parse(119.28, 26.08);
        System.out.println("-----》获取耗时2：" + calLastedTime(date1, new Date()) + "毫秒");
        // 获取省市区信息
        RegionInfo provInfo = location.getProv();
        RegionInfo cityInfo = location.getCity();
//        RegionInfo districtInfo = location.getDistrict();
        System.out.println("省编码：" + provInfo.getCode() +"名称：" + provInfo.getName());
        System.out.println("市编码：" + cityInfo.getCode() +"名称：" + cityInfo.getName());
    }

    /**
     * 执行时间(秒)
     *
     * @param startDate 开始时间
     * @param endDate   结束时间
     * @return 执行毫秒数
     */
    public static int calLastedTime(Date startDate, Date endDate) {
        long a = endDate.getTime();
        long b = startDate.getTime();
        return (int) (a - b);
    }

    public static DataSource buildDataSource() {
        MysqlDataSource dataSource = new MysqlDataSource();
        dataSource.setServerName("192.168.1.207");
        dataSource.setPort(3306);
        dataSource.setDatabaseName("zacf");
        dataSource.setUser("root");
        dataSource.setPassword("gawd110");
        try {
            Connection connection = dataSource.getConnection();
        } catch (SQLException e) {
            System.out.println("链接失败");
        }
        return dataSource;
    }
}
